package com.zhilianbao.leyaogo.ui.adapter.home.slider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.BalanceActivity;
import com.zhilianbao.leyaogo.utils.LogicCodeBlock;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.HomeTopBanner;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 首页Banner广告 ItemViewProvider
 */
public class HomeAppSliderViewProvider
        extends ItemViewProvider<HomeAppSlider, HomeAppSliderViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_home_banner, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull ViewHolder holder, @NonNull HomeAppSlider homeAppSlider) {
        //        List<String> picList = new ArrayList<>();
        //        for (HomeAppSlider.HomeAppSliderData.HomeAppSliderBean homeAppSliderBean : ) {
        //            picList.add(Utils.returnUrlShow(homeAppSliderBean.getImg()));
        //        }
        holder.mHomeTopBanner.setSource(homeAppSlider.getData().getList()).startScroll();
        holder.mHomeTopBanner.setOnItemClickL(position -> {
            switch (homeAppSlider.getData().getList().get(position).getType()) {
                //跳转到商品详情
                case 0:
                    Utils.startGoodsDetails((Activity) holder.mHomeTopBanner.getContext(), (int) homeAppSlider.getData().getList().get(position).getId(), (int) homeAppSlider.getData().getList().get(position).getGoodsSkuId(), false);
                    break;
                //跳转到商品列表
                case 1:
                    Utils.startGoodsClassifyList((Activity) holder.mHomeTopBanner.getContext(), homeAppSlider.getData().getList().get(position).getId(), homeAppSlider.getData().getList().get(position).getName());
                    break;

                //跳转到专题活动详情
                case 2:
                case 3:
                    Utils.startActivityDetails((Activity) holder.mHomeTopBanner.getContext(), homeAppSlider.getData().getList().get(position).getId(), false);
                    break;
                //充值有礼
                case 4:
                    if (Utils.isUserLogin(true, LogicCodeBlock.LogicState.Recharge.value)) {
                        Utils.startActivity((Activity) holder.mHomeTopBanner.getContext(), BalanceActivity.class);
                    } else {
                        LogicCodeBlock.create().registerLogicCodeCallback(logicState -> {
                                    if (logicState == LogicCodeBlock.LogicState.Recharge.value)
                                        Utils.startActivity((Activity) holder.mHomeTopBanner.getContext(), BalanceActivity.class);
                                }
                        );
                    }

                    break;
            }

        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        //首页Banner
        private HomeTopBanner mHomeTopBanner;

        ViewHolder(View itemView) {
            super(itemView);
            mHomeTopBanner = (HomeTopBanner) itemView.findViewById(R.id.home_top_banner);
        }
    }
}